package org.gacrnd.ind.mybatisdemo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@SpringBootApplication
public class MybatisdemoApplication {

    public static void main(String[] args) {
        //第一阶段: MyBatis初始化阶段
        String resource  = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //第二阶段: 数据读写阶段
        try(SqlSession session = sqlSessionFactory.openSession()){
            UserMapper mapper = session.getMapper(UserMapper.class);

            User userParam = new User();
            userParam.setSchoolName("Sunny School");

            List<User> userList = mapper.queryUserBySchoolName(userParam);
            for(User user: userList){
                System.out.println("name: "+ user.getName()+"; email: "+user.getEmail());
            }
        }

      //  SpringApplication.run(MybatisdemoApplication.class, args);
    }

}
